package com.example.zsf.base;


import android.view.View;

import com.blankj.utilcode.util.SPUtils;
import com.example.zsf.ZSFApplication;
import com.example.zsf.message.AccountEventMessage;
import com.example.zsf.model.UserModel;
import com.orhanobut.logger.Logger;

import org.greenrobot.eventbus.EventBus;

import java.util.Map;
import java.util.WeakHashMap;
import java.util.concurrent.locks.Lock;

import cn.jpush.android.api.JPushInterface;

public class ZSFGlobal {
    public static String baseUrl = "http://test.lb.ooen.cn";
    public static Long globalLastClickTime = 0L;

    public interface AccountCallBack {
        void noneauth();
    }


    private static Boolean isLogout = null;

    public static void login(String id, String token, String phone) {
        SPUtils.getInstance().put("id", id);
        SPUtils.getInstance().put("token", token);
        SPUtils.getInstance().put("phone", phone);


        UserModel userModel = new UserModel();
        userModel.setId(id);
        userModel.setToken(token);
        userModel.setPhone(phone);

        AccountEventMessage eventMessage = new AccountEventMessage();
        eventMessage.setLogin(true);
        eventMessage.setUserModel(userModel);

        JPushInterface.setAlias(ZSFApplication.getInstance(), 1, userModel.getToken());

        EventBus.getDefault().post(eventMessage);

        isLogout = false;
    }

    public static void logout() {
        SPUtils.getInstance().remove("id");
        SPUtils.getInstance().remove("token");
        SPUtils.getInstance().remove("phone", true);

        AccountEventMessage eventMessage = new AccountEventMessage();
        eventMessage.setLogin(false);
        eventMessage.setUserModel(null);

        JPushInterface.deleteAlias(ZSFApplication.getInstance(), 1);

        EventBus.getDefault().post(eventMessage);

        isLogout = true;
    }

    public static UserModel getUserModel() {
        UserModel userModel = new UserModel();
        userModel.setId(SPUtils.getInstance().getString("id"));
        userModel.setToken(SPUtils.getInstance().getString("token"));
        userModel.setPhone(SPUtils.getInstance().getString("phone"));
        return userModel;
    }


    public static boolean checkLogoutStatus(AccountCallBack callBack) {

        if (isLogout == null) {
            isLogout = SPUtils.getInstance().getString("token").equals("");
        }

        if (isLogout && callBack != null) {
            callBack.noneauth();
            return isLogout;
        }

        return isLogout;
    }

}
